import fl.motion.AdjustColor;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TransformGestureEvent;
import flash.filters.BitmapFilter;
import flash.filters.ColorMatrixFilter;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.geom.Vector3D;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.utils.ByteArray;

import mx.core.BitmapAsset;
import mx.events.FlexEvent;
import mx.states.AddChild;
import mx.utils.ObjectUtil;

import spark.components.Image;
import spark.primitives.BitmapImage;
import spark.primitives.Rect;
import spark.utils.BitmapUtil;

[Bindable]
[Embed(source="assets/filter/instagram/1977.png")]
private var filterAutomn:Class;

[Bindable]
[Embed(source="assets/filter/instagram/Amaro.png")]
private var filterOld:Class;

[Bindable]
[Embed(source="assets/filter/instagram/Brannan.png")]
private var filterRainbow:Class;

[Bindable]
[Embed(source="assets/filter/instagram/Nashville.png")]
private var filterSunset:Class;

[Bindable]
[Embed(source="assets/filter/custom/pink_round.png")]
private var filterPinkRound:Class;

[Bindable]
[Embed(source="assets/filter/custom/baige_round.png")]
private var filterBaigeRound:Class;

private var adjColor:AdjustColor = new AdjustColor();
protected function initProcess(event:FlexEvent):void
{
	// TODO Auto-generated method stub
	adjColor.brightness = 0;
	adjColor.contrast = 0;
	adjColor.hue = 0;
	adjColor.saturation = 0;
	
	hsAddEventListener();
	
	Multitouch.inputMode = MultitouchInputMode.GESTURE;
}


private function hsAddEventListener():void{
	hs_brightness.addEventListener(Event.CHANGE, adjustBrightness);
	hs_contrast.addEventListener(Event.CHANGE, adjustContrast);
}

private function adjustBrightness(e:Event):void{
	trace("bright adjusting slider moved");
	trace("selected value is : " + e.target.value.toString());
	adjColor.brightness = e.target.value;
	adjustSet();
}

private function adjustContrast(e:Event):void{
	adjColor.contrast = e.target.value;
	adjustSet();
}

private var filteredImageData:BitmapData; 
private var adjustedImageData:BitmapData;

private function adjustSet():void{
	var CMFilter:ColorMatrixFilter = new ColorMatrixFilter(adjColor.CalculateFinalFlatArray());
	
	//img_watermarked.filters = [CMFilter];
	
	if(adjustedImageData != null){
		adjustedImageData.applyFilter(filteredImageData, 
			new Rectangle(0, 0, filteredImageData.width, filteredImageData.height), 
			new Point(0,0), 
			CMFilter);
	}else{
		adjustedImageData = new BitmapData(filteredImageData.width, filteredImageData.height);
		adjustedImageData.applyFilter(filteredImageData, 
			new Rectangle(0, 0, filteredImageData.width, filteredImageData.height), 
			new Point(0,0), 
			CMFilter);
	}
	
	
	trace("filteringCount : " + filteringCount);
	img_watermarked.source = adjustedImageData;
	
}


private function onClickApplyWatermark(filter:String):void{
	
	var bd1:BitmapData = img_original.imageDisplay.bitmapData;
	//var bd2:BitmapData = img_watermark.imageDisplay.bitmapData;
	
	var bas:BitmapAsset;
	switch(filter){
		case 'pink':
			bas = BitmapAsset(new filterPinkRound() );
			break;
		case 'baige':
			bas = BitmapAsset(new filterBaigeRound() );
			break;
		
	}
	var bd2:BitmapData = bas.bitmapData;
	
	//bd2 = sourceZoom(bd2, true, 0.5, 0.5);
	bd2 = zoomToFill(bd2, bd1);
	bd1.merge(bd2, new Rectangle(0,0, bd2.width , bd2.height), new Point(0,0), 80,80,80,0);
	//bd1.draw(bd2, null, null, null, null, true);
	var b1:Bitmap = new Bitmap(bd1);
	
	filteredImageData = new BitmapData(bd2.width, bd2.height);
	filteredImageData = b1.bitmapData;
	filteringCount += 1;
	trace(filteredImageData.height);
	img_watermarked.source = b1;
	
	hs_brightness.value = 0;
	hs_contrast.value = 0;
}

private var filteringCount:int = 0;


private function zoomToFill(attachingBitmapData:BitmapData, targetBitmapData:BitmapData):BitmapData{
	
	
	var matrix:Matrix = new Matrix();
	var matriximage:BitmapData;
	
	var matrixScaleX:Number = targetBitmapData.width/attachingBitmapData.width;
	var matrixScaleY:Number = targetBitmapData.height/attachingBitmapData.height;
	
	matrix.a = matrixScaleX;
	matrix.d = matrixScaleY 
	
	
	
	matriximage = new BitmapData(attachingBitmapData.width*matrixScaleX, attachingBitmapData.height*matrixScaleY, false, 0x0000000);
	
	
	matriximage.draw(attachingBitmapData, matrix);                
	
	return matriximage;
}

private function sourceZoom(btmData:BitmapData, zoomType:Boolean, scaleX:Number, scaleY:Number):BitmapData{
	var matrixScaleX:Number  = 1;
	var matrixScaleY:Number  = 1;
	var baseScaleX:Number  = scaleX;
	var baseScaleY:Number = scaleY;
	
	var matrix:Matrix = new Matrix();
	var matriximage:BitmapData;
	
	
	if(zoomType){
		matrixScaleX = matrixScaleX + baseScaleX;
		matrixScaleY = matrixScaleY + baseScaleY;
		matrix.a = matrixScaleX;
		matrix.d = matrixScaleY 
	}else{
		matrixScaleX = matrixScaleX - baseScaleX;
		matrixScaleY = matrixScaleY - baseScaleY;
		matrix.a = matrixScaleX;
		matrix.d = matrixScaleY;    
	}
	
	
	matriximage = new BitmapData(btmData.width*matrixScaleX, btmData.height*matrixScaleY, false, 0x0000000);
	
	trace('MatrixScaleX: ' + matrixScaleX);
	trace('MatrixScaleY: ' + matrixScaleY);
	trace('BaseScaleX: ' + baseScaleX);
	trace('BaseScaleY: ' + baseScaleY);
	trace('Matrix: ' + ObjectUtil.toString(matrix));
	
	matriximage.draw(btmData, matrix);                
	
	return matriximage;
}





protected function img_watermarked_clickHandler(event:MouseEvent):void
{
	// TODO Auto-generated method stub
	this.currentState = "detail";
	img_detail.source = null;
	img_detail.source = img_watermarked.source;
}

protected function label1_clickHandler(event:MouseEvent):void
{
	// TODO Auto-generated method stub
	this.currentState = "edit";
}


protected function onGestureZoomDetailImage(event:TransformGestureEvent):void
{
	trace("zoom event occured."); 
	
	var diagonalScale:Number = new Number((event.scaleX + event.scaleY)/2); 
	
	
	img_detail.percentWidth *= diagonalScale;
	img_detail.percentHeight *= diagonalScale;
	
	
	
	// TODO Auto-generated method stub
	/*
	img_detail.transformAround(
		new Vector3D(
			event.localX, 
			event.localY, 
			0
		), 
		new Vector3D(
			Math.pow(img_detail.scaleX, 2), 
			Math.pow(img_detail.scaleY, 2), 
			0
		)
	);
	*/
}
